package fr.liglab.adele.icasa.rooms;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;

import fr.liglab.adele.icasa.device.DeviceListener;
import fr.liglab.adele.icasa.device.light.BinaryLight;
import fr.liglab.adele.icasa.device.presence.PresenceSensor;
import fr.liglab.adele.icasa.environment.SimulatedEnvironment;
import fr.liglab.adele.icasa.environment.SimulatedEnvironmentManager;
import fr.liglab.adele.icasa.interfaces.ILivingRoom;

@Component
@Provides
public class LivingRoom implements DeviceListener, ILivingRoom{
	
	@Requires(filter="(device.serialNumber=binarylight_lr)")
	BinaryLight binarylight;
	
	@Requires(filter="(device.serialNumber=presencesensor_lr)")
	PresenceSensor presencesensor;
	
	@Requires(filter="(environment.id=envLivingRoom)")
	SimulatedEnvironment env;
	
	@Requires
	SimulatedEnvironmentManager env_manager;
	
	String place="Living Room";
	
	@Validate
	public void starting() {    
		env_manager.bindDeviceToEnvironment("binarylight_lr", "envLivingRoom");
		env_manager.bindDeviceToEnvironment("presencesensor_lr", "envLivingRoom");
		        
        presencesensor.addListener(this);
        
        System.out.println(" ---------> "+place+" ACTIVE <--------- " );
    }
	
	@Invalidate
	public void stopping() {    
        System.out.println( " ---------> "+place+" INACTIVE <--------- " );        
    }

	public void notifyDeviceEvent() {
		binarylight.setPowerStatus(presencesensor.getSensedPresence());		
		System.out.println(" <"+place+"> Light "+(binarylight.getPowerStatus()?"ON":"OFF"));
	}

	
	public void setPresence(double nbPeople){
		env.setProperty(SimulatedEnvironment.PRESENCE, nbPeople);
	}

}
